package question

import (
	"errors"
	"github.com/gin-gonic/gin"
	"smartLearn/models"
	"smartLearn/modules/response"
	"smartLearn/public/common"
	"strconv"
	"strings"
)

type QuestionInput struct {
	SubjectId        int    `json:"subjectId" `
	TypeId       int    `json:"typeId" `
	SourceId       int    `json:"sourceId" `
	DifStatus       int    `json:"difStatus" `
	StarStatus       int    `json:"starStatus" `
	VerifyStatus       int    `json:"verifyStatus" `
	ValidStatus       int    `json:"validStatus" `
	TestStatus       int    `json:"testStatus" `
	Comment        string    `json:"comment" `
	Question        string    `json:"question" `
	Answer        string    `json:"answer" `
	LevelIds        string    `json:"levelIds" `
	KnowledgeIds        string    `json:"knowledgeIds" `
	ImgIds	 string    `json:"imgIds" `
}
func Add(c *gin.Context) {
	var questionInput QuestionInput
	err:=c.BindJSON(&questionInput)
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}

	//读取相关的阶段信息
	levelIds:=strings.Split(questionInput.LevelIds,",")
	levelIdsArr := make([]int, len(levelIds))

	for i, v := range levelIds {
		levelIdsArr[i], err = strconv.Atoi(v)
		if err != nil {
			continue //如果传入非数组，那么忽略
		}
	}

	//读取相关的阶段信息
	knowledgeIds:=strings.Split(questionInput.KnowledgeIds,",")
	knowledgeIdsArr := make([]int, len(knowledgeIds))

	for i, v := range knowledgeIds {
		knowledgeIdsArr[i], err = strconv.Atoi(v)
		if err != nil {
			continue //如果传入非数组，那么忽略
		}
	}

	//读取相关的ImgID的信息
	ImgIds:=strings.Split(questionInput.ImgIds,",")

	questionModel := models.SystemQuestion{};
	questionModel.Question=questionInput.Question
	questionModel.Answer=questionInput.Answer
	questionModel.Comment=questionInput.Comment
	questionModel.SubjectId=questionInput.SubjectId
	questionModel.TypeId=questionInput.TypeId
	questionModel.SourceId=questionInput.SourceId
	questionModel.DifStatus=questionInput.DifStatus
	questionModel.StarStatus=questionInput.StarStatus
	questionModel.ValidStatus=questionInput.ValidStatus
	questionModel.VerifyStatus=questionInput.VerifyStatus

	newid,err:=questionModel.Add(levelIdsArr,knowledgeIdsArr,ImgIds)
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}
	var data = make(map[string]interface{}, 0)
	data["id"]=newid
	response.ShowData(c,data)

}

func Update(c *gin.Context) {
	var questionInput QuestionInput
	id,err:=strconv.Atoi(c.Param("id"))
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}
	err=c.BindJSON(&questionInput)
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}

	//读取相关的阶段信息
	levelIds:=strings.Split(questionInput.LevelIds,",")
	levelIdsArr := make([]int, len(levelIds))

	for i, v := range levelIds {
		levelIdsArr[i], err = strconv.Atoi(v)
		if err != nil {
			continue //如果传入非数组，那么忽略
		}
	}

	//读取相关的阶段信息
	knowledgeIds:=strings.Split(questionInput.KnowledgeIds,",")
	knowledgeIdsArr := make([]int, len(knowledgeIds))

	for i, v := range knowledgeIds {
		knowledgeIdsArr[i], err = strconv.Atoi(v)
		if err != nil {
			continue //如果传入非数组，那么忽略
		}
	}

	//读取相关的ImgID的信息
	ImgIds:=strings.Split(questionInput.ImgIds,",")


	questionModel := models.SystemQuestion{}
	questionModel.Id=id
	questionModel.Question=questionInput.Question
	questionModel.Answer=questionInput.Answer
	questionModel.Comment=questionInput.Comment
	questionModel.SubjectId=questionInput.SubjectId
	questionModel.TypeId=questionInput.TypeId
	questionModel.SourceId=questionInput.SourceId
	questionModel.DifStatus=questionInput.DifStatus
	questionModel.StarStatus=questionInput.StarStatus
	questionModel.ValidStatus=questionInput.ValidStatus
	questionModel.VerifyStatus=questionInput.VerifyStatus

	newid,err:=questionModel.Update(levelIdsArr,knowledgeIdsArr,ImgIds)
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}
	var data = make(map[string]interface{}, 0)
	data["id"]=newid
	response.ShowData(c,data)

}

func List(c *gin.Context) {
	questionIds:=c.Query("questionIds")
	pageNum,err := strconv.ParseInt(c.Query("pageNum"), 10, 64)
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}

	pageSize,err  := strconv.ParseInt(c.Query("pageSize"), 10, 64)
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}

	paging:=&common.Paging{Page:pageNum,PageSize:pageSize}

	testStatus:= c.Query("testStatus")
	userId:= c.Query("userId")

	if questionIds!=""{
		//那么按照题号查询，其它条件忽略
		listByQuestionIds(c,questionIds,testStatus,userId,paging)
		return
	}
	//默认值是有效，并且审核通过的
	questionModel:= models.SystemQuestion{VerifyStatus:1,ValidStatus:1}
	subjectId,err:=strconv.Atoi(c.Query("subjectId"))
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}
	questionModel.SubjectId=subjectId
	levelIds:=c.Query("levelIds")
	if levelIds==""{
		err=errors.New("请输入阶段")
		response.ShowError(c,err.Error())
		return
	}
	knowledgeIds:=c.Query("knowledgeIds")

	typeId:=c.Query("typeId")
	if typeId!=""{
		questionModel.TypeId,_=strconv.Atoi(typeId)
	}
	sourceId:=c.Query("sourceId")
	if sourceId!=""{
		questionModel.SourceId,_=strconv.Atoi(sourceId)
	}
	questionModel.Comment =c.Query("comment")

	difStatus:=c.Query("difStatus")
	if difStatus!=""{
		questionModel.DifStatus,_=strconv.Atoi(difStatus)
	}
	starStatus:=c.Query("starStatus")
	if starStatus!=""{
		questionModel.StarStatus,_=strconv.Atoi(starStatus)
	}
	verifyStatus:=c.Query("verifyStatus")
	if verifyStatus!=""{
		questionModel.VerifyStatus,_=strconv.Atoi(verifyStatus)
	}
	validStatus:=c.Query("validStatus")
	if validStatus!=""{
		questionModel.ValidStatus,_=strconv.Atoi(validStatus)
	}


	listByQuestionInput(c,questionModel,levelIds,knowledgeIds,testStatus,userId,paging)
}

func listByQuestionIds(c *gin.Context,questionIds string,testStatus string, userId string,paging *common.Paging) {
	questionModel := models.SystemQuestion{};
	clientQuestions,err:=questionModel.GetSimpleByQuestionIds(questionIds,testStatus,userId,paging)
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}
	var data = make(map[string]interface{}, 0)
	data["total"]=paging.Total
	data["list"]=clientQuestions
	response.ShowData(c,data)
}
func listByQuestionInput(c *gin.Context,q models.SystemQuestion,levelIds string,knowledgeIds string,testStatus string,userId string,paging *common.Paging) {

	clientQuestions,err:=q.GetSimpleByQuestionInput(levelIds,knowledgeIds,testStatus,userId,paging)
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}
	var data = make(map[string]interface{}, 0)
	data["total"]=paging.Total
	data["list"]=clientQuestions
	response.ShowData(c,data)
}


func GetById(c *gin.Context) {
	id,err:=strconv.Atoi(c.Param("id"))
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}
	questionModel := models.SystemQuestion{};
	question,err:=questionModel.GetById(id)
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}

	var data = make(map[string]interface{}, 0)


	data["subjectId"]= question.SubjectId
	data["typeId"]= question.TypeId
	data["sourceId"]= question.SourceId
	data["difStatus"]= question.DifStatus
	data["starStatus"]= question.StarStatus
	data["verifyStatus"]= question.VerifyStatus
	data["validStatus"]= question.ValidStatus
	data["comment"]= question.Comment
	data["question"]= question.Question
	data["answer"]= question.Answer

	questionKnowledge:=models.SystemQuestionKnowledge{}
	knowledgeIdArr,err:=questionKnowledge.GetByQuestionId(id)
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}
	data["knowledge"]=knowledgeIdArr

	questionLevel:=models.SystemQuestionLevel{}
	levelIdArr,err:=questionLevel.GetByQuestionId(id)
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}
	data["level"]=levelIdArr
	response.ShowData(c,data)

}
func Delete(c *gin.Context) {
	id, err := strconv.Atoi(c.Param("id"))
	if err != nil {
		response.ShowError(c, err.Error())
		return
	}
	questionModel:=models.SystemQuestion{}
	questionModel.Id=id
	err=questionModel.DeleteById()
	if err != nil {
		response.ShowError(c, err.Error())
		return
	}
	response.ShowSuccess(c,"删除成功")
}

func UpdateVerifiyStatus(c *gin.Context) {

	verifyStatus, err := strconv.Atoi(c.Query("verifyStatus"))
	if err != nil {
		response.ShowError(c, err.Error())
		return
	}
	if  verifyStatus!=1 && verifyStatus!=0{
		response.ShowError(c, errors.New("审核状态不合法").Error())
		return

	}

	ids:=c.Query("ids")
	questionModel:=models.SystemQuestion{}
	err=questionModel.UpdateVerifyStatus(ids,verifyStatus)
	if err != nil {
		response.ShowError(c, err.Error())
		return
	}
	response.ShowSuccess(c,"更新审核状态成功")
}

func UpdateValidStatus(c *gin.Context) {

	validStatus, err := strconv.Atoi(c.Query("validStatus"))
	if err != nil {
		response.ShowError(c, err.Error())
		return
	}
	if  validStatus!=1 && validStatus!=0{
		response.ShowError(c, errors.New("有效状态不合法").Error())
		return

	}

	ids:=c.Query("ids")
	questionModel:=models.SystemQuestion{}
	err=questionModel.UpdateValidStatus(ids,validStatus)
	if err != nil {
		response.ShowError(c, err.Error())
		return
	}
	response.ShowSuccess(c,"更新审核状态成功")
}
func UpdateTestStatus(c *gin.Context) {

	testStatus, err := strconv.Atoi(c.Query("testStatus"))
	if err != nil {
		response.ShowError(c, err.Error())
		return
	}

	userId, err := strconv.Atoi(c.Query("userId"))
	if err != nil {
		response.ShowError(c, err.Error())
		return
	}
	questionId, err := strconv.Atoi(c.Query("questionId"))
	if err != nil {
		response.ShowError(c, err.Error())
		return
	}
	answerHis:=c.Query("answerHis")

	//读取相关的ImgID的信息
	ImgIds:=strings.Split(c.Query("imgIds"),",")


	userQuestionModel:=models.UserQuestion{}
	userQuestionModel.UserId=userId

	userQuestionModel.QuestionId=questionId
	userQuestionModel.AnswerHis=answerHis
	userQuestionModel.TestStatus=testStatus
	err=userQuestionModel.UpdateTestStatus(ImgIds)
	if err != nil {
		response.ShowError(c, err.Error())
		return
	}
	response.ShowSuccess(c,"更新做题情况成功")
}
func ListTestStatus(c *gin.Context) {
	questionId,err := strconv.Atoi(c.Query("questionId"))
	if err!=nil{
		response.ShowError(c,err.Error())
	}
	userId,err := strconv.Atoi(c.Query("userId"))
	if err!=nil{
		response.ShowError(c,err.Error())
	}

	userQuestionModel:=models.UserQuestion{}
	userQuestionModel.UserId=userId
	userQuestionModel.QuestionId=questionId
	userQuestions,err:= userQuestionModel.ListTestStatus()
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}
	var data = make(map[string]interface{}, 0)
	if len(userQuestions)==0{
		data["testStatus"]=0
		data["answerHis"]=""

	}else{

		data["answerHis"]=userQuestions[0].AnswerHis
		data["testStatus"]=userQuestions[0].TestStatus
	}
	response.ShowData(c,data)
}


func ListQuestions(c *gin.Context) {
	questionIds := c.Query("questionIds")


	systemQuestionModel:=models.SystemQuestion{}

	questions,err:=systemQuestionModel.GetQuestionByQuestionIds(questionIds)
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}
	var data = make(map[string]interface{}, 0)
	data["list"]=questions
	response.ShowData(c,data)
}

func ListAnswers(c *gin.Context) {
	questionIds := c.Query("questionIds")


	systemQuestionModel:=models.SystemQuestion{}

	questions,err:=systemQuestionModel.GetAnswerByQuestionIds(questionIds)
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}
	var data = make(map[string]interface{}, 0)
	data["list"]=questions
	response.ShowData(c,data)
}


